const { noticeApi } = require('../../../../utils/api');
const tips = require('../../../../utils/tips');

Page({
  data: {
    formData: {
      title: '',
      content: '',
      classId: '',
      isTop: false
    },
    classList: [
      { id: 2504, name: '六年级一班' },
      { id: 2505, name: '六年级二班' },
      { id: 2506, name: '六年级三班' }
    ],
    selectedClass: {},
    classIndex: 0,
    submitting: false,
    canSubmit: false
  },

  onLoad() {
    this.checkFormValid();
  },

  // 输入框变化
  onInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    this.setData({
      [`formData.${field}`]: value
    });
    
    this.checkFormValid();
  },

  // 班级选择
  onClassChange(e) {
    const index = e.detail.value;
    const selectedClass = this.data.classList[index];
    
    this.setData({
      classIndex: index,
      selectedClass: selectedClass,
      'formData.classId': selectedClass.id
    });
    
    this.checkFormValid();
  },

  // 通知类型选择
  onTypeChange(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      'formData.type': type
    });
  },

  // 开关变化
  onSwitchChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    this.setData({
      [`formData.${field}`]: value
    });
  },

  // 检查表单是否有效
  checkFormValid() {
    const { title, content } = this.data.formData;
    const canSubmit = title.trim() && content.trim();
    
    this.setData({ canSubmit });
  },

  // 提交表单
  async onSubmit(e) {
    if (!this.data.canSubmit || this.data.submitting) {
      return;
    }
    this.setData({ submitting: true });
    try {
      const noticeData = {
        classId: Number(this.data.formData.classId),
        publisherUserId: 10001, // TODO: 替换为实际登录老师ID
        title: this.data.formData.title,
        content: this.data.formData.content,
        isTop: this.data.formData.isTop ? 1 : 0
      };
      console.log('发布通知数据:', noticeData);
      const result = await noticeApi.addNotice(noticeData);
      if (result) {
        wx.showToast({ title: '发布成功', icon: 'success' });
        setTimeout(() => { wx.navigateBack(); }, 1500);
      } else {
        throw new Error('发布失败');
      }
    } catch (error) {
      console.error('发布通知失败:', error);
      wx.showToast({ title: '发布失败，请重试', icon: 'none' });
    } finally {
      this.setData({ submitting: false });
    }
  },

  // 保存草稿
  saveDraft() {
    const { title, content } = this.data.formData;
    
    if (!title.trim() && !content.trim()) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }

    // 这里可以实现草稿保存逻辑
    wx.showToast({
      title: '草稿保存功能开发中',
      icon: 'none'
    });
  },

  // 发布通知时
  onSendNotice(theme) {
    wx.showModal({
      title: '发送确认',
      content: tips.notice.send(theme),
      confirmText: '同时发送到群',
      cancelText: '仅通知',
      success(res) {
        // 处理发送逻辑
      }
    });
  },
  // 未读提醒
  onUnreadRemind(theme, count, summary) {
    wx.showModal({
      title: '未读提醒',
      content: tips.notice.unread(theme, count, summary),
      confirmText: '发送提醒',
      cancelText: '不用'
    });
  }
});
